﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNC.Media.KQXS.DataAccess.BL;
using ThanhTu.GNC.Media.KQXS.Common;
using System.Data;

namespace GNCMedia.KQXS.MenuPage
{
    public partial class ReportZeroToNine : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ProvinceBL vProvinceBL = new ProvinceBL();
            ResultLotteryBL vResultLotteryBL = new ResultLotteryBL();
            try
            {
                if (!Page.IsPostBack)
                {
                    string thang = string.Empty;
                    ddl_province.DataSource = vProvinceBL.selectadd();
                    ddl_province.DataTextField = "princeName";
                    ddl_province.DataValueField = "provinceID";
                    ddl_province.DataBind();

                    if (Convert.ToInt32(DateTime.Now.Month.ToString()) < 10)
                    {
                        thang = "0" + DateTime.Now.Month.ToString();
                    }
                    else
                    {
                        thang = DateTime.Now.Month.ToString();
                    }
                    ddl_denngay.SelectedValue = DateTime.Now.Day.ToString();
                    ddl_denthang.SelectedValue = thang;
                    ddl_dennam.SelectedValue = DateTime.Now.Year.ToString();
                    ddl_tunam.SelectedValue = DateTime.Now.Year.ToString();

                    string sto_date = ddl_tungay.SelectedValue + "/" + ddl_tuthang.SelectedValue + "/" + ddl_tunam.SelectedValue;
                    string sfrom_date = ddl_denngay.SelectedValue + "/" + ddl_denthang.SelectedValue + "/" + ddl_dennam.SelectedValue;
                    DateTime to_date = Utils.ToDateTime(sto_date);
                    DateTime from_date = Utils.ToDateTime(sfrom_date);
                    string provinceId = ddl_province.SelectedValue.ToString();
                    int count = 0;
                    DataTable tbl = vResultLotteryBL.Statistic_0_To_9(ddl_province.SelectedValue, to_date, from_date, 1, 10, ref count); ;
                    int maxCol2 = 0;
                    int maxCol6 = 0;
                    int maxCol7 = 0;
                    string smaxcol2 = string.Empty;
                    string smaxcol6 = string.Empty;
                    string smaxcol7 = string.Empty;
                    foreach (DataRow row in tbl.Rows)
                    {
                        if (Convert.ToInt32(row["Col2"].ToString()) >= maxCol2)
                        {
                            maxCol2 = Convert.ToInt32(row["Col2"].ToString());
                            smaxcol2 = smaxcol2 + "," + row["Col1"].ToString();
                        }
                        if (Convert.ToInt32(row["Clo6"].ToString()) >= maxCol6)
                        {
                            maxCol6 = Convert.ToInt32(row["Clo6"].ToString());
                            smaxcol6 = smaxcol6 + "," + row["Col1"].ToString();
                        }
                        if (Convert.ToInt32(row["Clo7"].ToString()) >= maxCol7)
                        {
                            maxCol7 = Convert.ToInt32(row["Clo7"].ToString());
                            smaxcol7 = smaxcol7 + "," + row["Col1"].ToString();
                        }
                    }


                    lbl_max2.Text = smaxcol2.Substring(1);
                    lbl_max6.Text = smaxcol6.Substring(1);
                    lbl_max7.Text = smaxcol7.Substring(1);
                    rpt_Statistic_0_To_9.DataSource = vResultLotteryBL.Statistic_0_To_9(ddl_province.SelectedValue, to_date, from_date, 1, 10, ref count);
                    rpt_Statistic_0_To_9.DataBind();
                    lbl_todate1.Text = sto_date;
                    lbl_fromdate2.Text = sfrom_date;
                    lbl_nameprovince.Text = ddl_province.SelectedItem.Text;
                    lbl_todate.Text = sto_date;
                    lbl_from_date.Text = sfrom_date;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ResultLotteryBL vResultLotteryBL = new ResultLotteryBL();
            try
            {
                string sto_date = ddl_tungay.SelectedValue + "/" + ddl_tuthang.SelectedValue + "/" + ddl_tunam.SelectedValue;
                string sfrom_date = ddl_denngay.SelectedValue + "/" + ddl_denthang.SelectedValue + "/" + ddl_dennam.SelectedValue;
                DateTime to_date = Utils.ToDateTime(sto_date);
                DateTime from_date = Utils.ToDateTime(sfrom_date);
                string provinceId = ddl_province.SelectedValue.ToString();
                int count = 0;
                DataTable tbl = vResultLotteryBL.Statistic_0_To_9(ddl_province.SelectedValue, to_date, from_date, 1, 10, ref count); ;
                int maxCol2 = 0;
                int maxCol6 = 0;
                int maxCol7 = 0;
                string smaxcol2 = string.Empty;
                string smaxcol6 = string.Empty;
                string smaxcol7 = string.Empty;
                foreach (DataRow row in tbl.Rows)
                {
                    if (Convert.ToInt32(row["Col2"].ToString()) >= maxCol2)
                    {
                        maxCol2 = Convert.ToInt32(row["Col2"].ToString());
                        smaxcol2 = smaxcol2 + "," + row["Col1"].ToString();
                    }
                    if (Convert.ToInt32(row["Clo6"].ToString()) >= maxCol6)
                    {
                        maxCol6 = Convert.ToInt32(row["Clo6"].ToString());
                        smaxcol6 = smaxcol6 + "," + row["Col1"].ToString();
                    }
                    if (Convert.ToInt32(row["Clo7"].ToString()) >= maxCol7)
                    {
                        maxCol7 = Convert.ToInt32(row["Clo7"].ToString());
                        smaxcol7 = smaxcol7 + "," + row["Col1"].ToString();
                    }
                }

                lbl_max2.Text = smaxcol2.Substring(1);
                lbl_max6.Text = smaxcol6.Substring(1);
                lbl_max7.Text = smaxcol7.Substring(1);
                rpt_Statistic_0_To_9.DataSource = vResultLotteryBL.Statistic_0_To_9(ddl_province.SelectedValue, to_date, from_date, 1, 10, ref count);
                rpt_Statistic_0_To_9.DataBind();
                lbl_todate1.Text = sto_date;
                lbl_fromdate2.Text = sfrom_date;
                lbl_nameprovince.Text = ddl_province.SelectedItem.Text;
                lbl_todate.Text = sto_date;
                lbl_from_date.Text = sfrom_date;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}